package com.nuoye.camera2studydemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Consumer;
import androidx.databinding.DataBindingUtil;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.nuoye.camera2studydemo.databinding.ActivityMainBinding;
import com.nuoye.camera2studydemo.fragment.CameraPreviewAFFragment;
import com.nuoye.camera2studydemo.fragment.CameraPreviewFragment;
import com.permissionx.guolindev.PermissionX;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<ChickConsumer> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        items = getShowList();
        binding.listMain.setAdapter(new ArrayAdapter<ChickConsumer>(this,android.R.layout.simple_expandable_list_item_1,items));
        binding.listMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ChickConsumer chickItem = items.get(i);
                chickItem.getConsumer().accept("");
            }
        });
    }

    private List<ChickConsumer> getShowList() {
        List<ChickConsumer> showItems= new ArrayList<>();
        showItems.add(new ChickConsumer("申请权限", s -> PermissionX.init(MainActivity.this)
        .permissions(Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO)
        .onExplainRequestReason((scope, deniedList) -> {

        }).request((allGranted, grantedList, deniedList) -> Toast.makeText(MainActivity.this,"获取完成",Toast.LENGTH_SHORT).show())));
        showItems.add(new ChickConsumer("CameraManager 方法调用", new Consumer<String>() {
            @Override
            public void accept(String s) {
                ShowFragmentActivity.open(MainActivity.this,CameraMangerFragment.class.getName());
            }
        }));
        showItems.add(new ChickConsumer("CameraPreviewFragment,摄像头预览", new Consumer<String>() {
            @Override
            public void accept(String s) {
                ShowFragmentActivity.open(MainActivity.this, CameraPreviewFragment.class.getName());
            }
        }));
        showItems.add(new ChickConsumer("CameraPreviewFragment,摄像头预览,对焦", new Consumer<String>() {
            @Override
            public void accept(String s) {
                ShowFragmentActivity.open(MainActivity.this, CameraPreviewAFFragment.class.getName());
            }
        }));

        return showItems;
    }
}